package com.hainatong.shiphelper.module.warehouse;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;
import androidx.appcompat.widget.Toolbar;
import androidx.core.content.ContextCompat;
import androidx.databinding.DataBindingUtil;
import com.blankj.utilcode.util.ObjectUtils;
import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.hainatong.shiphelper.R;
import com.hainatong.shiphelper.databinding.ActivityReceiveHistoryBinding;
import com.hainatong.shiphelper.databinding.HeaderLyrecordListViewBinding;
import com.hainatong.shiphelper.module.warehouse.adapter.EquipmentRecordAdapter;
import com.hainatong.shiphelper.module.warehouse.entity.WorkerRecordBean;
import com.hainatong.shiphelper.module.warehouse.viewmodel.LinYongRecordViewModel;
import com.hainatong.shiphelper.utils.GsonUtil;
import com.hainatong.shiphelper.utils.RecyclerViewHelper;
import com.hainatong.mvvmbase.base.BaseMvvmActivity;
import com.hainatong.mvvmbase.utils.StatusBarUtils;
import com.scwang.smart.refresh.footer.ClassicsFooter;
import com.scwang.smart.refresh.header.ClassicsHeader;

public class LinYongDetailActivity extends BaseMvvmActivity<ActivityReceiveHistoryBinding, LinYongRecordViewModel>
{
    public static String WORKER_RECORD_TAG = "WorkerRecord";

    private WorkerRecordBean worker;

    private EquipmentRecordAdapter adapter;

    private HeaderLyrecordListViewBinding headerBinding;

    public static void startByParam(Activity mContext, String workerStr)
    {
        Bundle bundle = new Bundle();
        bundle.putString(LinYongDetailActivity.WORKER_RECORD_TAG,workerStr);
        start(mContext, bundle, false);
    }

    public static void start(Activity mContext, Bundle bundle)
    {
        start(mContext, bundle, false);
    }

    public static void start(Activity mContext, Bundle bundle, boolean isFinish)
    {
        Intent intent = new Intent();
        intent.setClass(mContext, LinYongDetailActivity.class);
        if (bundle != null) intent.putExtra(mContext.getPackageName(), bundle);
        mContext.startActivity(intent);
        if (isFinish)
        {
            mContext.finish();
        }
    }

    @Override
    protected int getLayoutId()
    {
        return R.layout.activity_receive_history;
    }

    @Override
    protected int getBindingVariable() {
        return 0;
    }

    @Override
    protected void initStatusBar()
    {
        // 设置透明状态栏，兼容4.4
        // 设置状态栏为白底黑字
        StatusBarUtils.setStatusBarColor(this, Color.parseColor("#FF8C3D"));
        StatusBarUtils.setStatusBarDarkFont(this, false);
    }

    public Bundle getBundle()
    {
        if (getIntent() != null && getIntent().hasExtra(getPackageName()))
            return getIntent().getBundleExtra(getPackageName());
        else
            return null;
    }

    @Override
    protected void initView()
    {
        Bundle bundle = getBundle();
        String jsonStr = bundle.getString(LinYongDetailActivity.WORKER_RECORD_TAG, "");
        if( !StringUtils.isEmpty( jsonStr ) )
        {
            worker = GsonUtil.json2Obj( jsonStr,WorkerRecordBean.class );
        }
        // 表头
        headerBinding = DataBindingUtil.inflate(getLayoutInflater(),R.layout.header_lyrecord_list_view, (ViewGroup) viewDataBinding.recyclerview.getParent(),false);

        viewDataBinding.includeTitle.toolbar.setBackgroundColor(ContextCompat.getColor( this,R.color.red_oval ));
        setToolbarTitle(viewDataBinding.includeTitle.toolbar,getString( R.string.fun_linyongjilu )  );
        setToolBar( viewDataBinding.includeTitle.toolbar );

        viewDataBinding.refreshlayout.setRefreshHeader( new ClassicsHeader(this));
        viewDataBinding.refreshlayout.setRefreshFooter( new ClassicsFooter(this));
        viewDataBinding.refreshlayout.setOnRefreshListener(refreshLayout -> {
           viewModel.tryRefreshLyRecord();
        });
        viewDataBinding.refreshlayout.setOnLoadMoreListener( refreshLayout -> {
           viewModel.loadMoreRecord();
        } );

        adapter = new EquipmentRecordAdapter();

        RecyclerViewHelper.addLinerLayoutMgrNoDecoration( viewDataBinding.recyclerview );
        viewDataBinding.recyclerview.setAdapter( adapter );

        if(ObjectUtils.isNotEmpty( worker ))
        {
            headerBinding.setWorker( worker );
            headerBinding.executePendingBindings();
        }
        adapter.addHeaderView( headerBinding.getRoot() );
    }

    @Override
    protected void initData()
    {
        if( ObjectUtils.isNotEmpty( worker ) )
        {
            viewModel.queryUserLYRecordByUuid( worker.getUuid() );
        }

        //请求返回的 记录
        viewModel.getLinYongRecordResult().observe(this,result -> {
            if( result.isSuccess() )
            {
                if( result.isRefresh() )
                {
                    viewDataBinding.refreshlayout.finishRefresh(true);
                    if( result.getData() != null )
                    {
                        adapter.setList(result.getData());
                    }
                }
                else
                {
                    viewDataBinding.refreshlayout.finishLoadMore(true);
                    if( result.getData() != null )
                    {
                        adapter.addData(result.getData());
                    }
                }
            }
            else
            {
                if( result.isRefresh() )
                {
                    //刷新时 加载失败
                    ToastUtils.showShort( result.getMsg() );
                }
                else
                {
                    if( result.isLoadMoreEmpty() )
                    {
                        viewDataBinding.refreshlayout.finishLoadMoreWithNoMoreData();
                    }
                }
            }
        });

    }

    @Override
    protected void onRetryBtnClick()
    {

    }

    private void setToolbarTitle(Toolbar toolbar, String title)
    {
        // toolbar.setBackgroundColor(ContextCompat.getColor(this,R.color.red));

        ((TextView)toolbar.findViewById(R.id.tv_title)).setText(title);
        ((TextView)toolbar.findViewById(R.id.tv_title)).setTextColor(ContextCompat.getColor(this,R.color.colorwhite));
        ((ImageButton)toolbar.findViewById(R.id.btn_back)).setImageResource(R.drawable.svg_back_white);
        toolbar.findViewById(R.id.btn_back).setOnClickListener(v -> {
            supportFinishAfterTransition();
        });
    }




}
